package com.sky.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sky.blog.dao.enums.StatusCode;
import com.sky.blog.dao.mapper.BlogCategoryMapper;
import com.sky.blog.dao.pojo.BlogCategory;
import com.sky.blog.dao.vo.BlogHeaderCategoryVo;
import com.sky.blog.service.BlogCategoryService;
import com.sky.blog.util.DozerUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sky  2022/3/28
 * @version 1.0
 */
@Service
public class BlogCategoriesImpl implements BlogCategoryService {

    @Resource
    private BlogCategoryMapper blogCategoryMapper;

    @Override
    public List<BlogHeaderCategoryVo> getBlogHeaderCategories() {
        LambdaQueryWrapper<BlogCategory> wrapper = Wrappers.lambdaQuery(BlogCategory.class);
        wrapper.select(BlogCategory::getId, BlogCategory::getTitle);
        wrapper.eq(BlogCategory::getStatus, StatusCode.NORMAL);
        wrapper.orderByDesc(BlogCategory::getSort)
                .orderByDesc(BlogCategory::getGmtCreated);
        return blogCategoryMapper.selectList(wrapper)
                .stream().map(category -> DozerUtils.copy(category, BlogHeaderCategoryVo.class))
                .collect(Collectors.toList());
    }
}
